﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace MyFile.Utils
{
    public class HttpUtils
    {
        public static string DoPost(string url, string body, [Optional, DefaultParameterValue(null)] Dictionary<string, string> header, string contentType = "application/json")
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.KeepAlive = false;
                request.ContentType = contentType;
                request.Method = "POST";
                request.Timeout = 3000;
                request.ContentLength = 0L;
                if (header != null)
                {
                    foreach (KeyValuePair<string, string> pair in header)
                    {
                        request.Headers.Add(pair.Key, pair.Value);
                    }
                }
                if (!string.IsNullOrWhiteSpace(body))
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(body);
                    request.ContentLength = bytes.Length;
                    using (Stream stream = request.GetRequestStream())
                    {
                        stream.Write(bytes, 0, bytes.Length);
                        stream.Close();
                    }
                }
                HttpWebResponse result = (HttpWebResponse)System.Threading.Tasks.Task.Factory.FromAsync<WebResponse>(new Func<AsyncCallback, object, IAsyncResult>(request.BeginGetResponse), new Func<IAsyncResult, WebResponse>(request.EndGetResponse), null).Result;
                using (StreamReader reader = new StreamReader(result.GetResponseStream()))
                {
                    return reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return "0";
            }
        }
        public static string DoGet(string url, [Optional, DefaultParameterValue(null)] Dictionary<string, string> header, string contentType = "application/json")
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.KeepAlive = true;
            request.ContentType = contentType;
            request.Method = "GET";
            request.Timeout = 3000;
            request.ContentLength = 0L;
            if (header != null)
            {
                foreach (KeyValuePair<string, string> pair in header)
                {
                    request.Headers.Add(pair.Key, pair.Value);
                }
            }
            HttpWebResponse result = (HttpWebResponse)System.Threading.Tasks.Task.Factory.FromAsync<WebResponse>(new Func<AsyncCallback, object, IAsyncResult>(request.BeginGetResponse), new Func<IAsyncResult, WebResponse>(request.EndGetResponse), null).Result;
            using (StreamReader reader = new StreamReader(result.GetResponseStream()))
            {
                return reader.ReadToEnd();
            }
        }
    }
}
